﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.IO;

namespace TinyCinema.Data
{
    public class CustomerType : Execution
    {
        public string ID { get; set; }
        string _Name;
        public string Name 
        { get
            {
                return _Name;
            } 
            set
            {
                _Name=value;
                OnPropertyChanged("Name");
            }
        }
        int _MinPoint;
        public int MinPoint 
        { 
            get{ return _MinPoint;}
            set 
            {
                _MinPoint= value;
                OnPropertyChanged("MinPoint");
            }
        }
        double _Discount;
        public double Discount 
        {
            get { return _Discount; }
            set
            {
                _Discount = value;
                OnPropertyChanged("Discount");
            }
        }
        public Image Picture { get; set; }
        public double PromotionDiscount { get; set; }
        public ObservableCollection<Customer> Customers { get; set; }
        public ObservableCollection<Promotion> Promotions { get; set; }
        public static ObservableCollection<CustomerType> lstCustomerType { get; set; }
        int _Index { get; set; }
        public int Index
        {
            get { return _Index; }
            set
            {
                ID = lstCustomerType[value].ID;
                Name = lstCustomerType[value].Name;
                Picture = lstCustomerType[value].Picture;
                MinPoint = lstCustomerType[value].MinPoint;
                Discount = lstCustomerType[value].Discount;
                _Index = value;
            }
        }
        public CustomerType()
        {

            Picture = new Image();
            if (lstCustomerType == null)
            {
                lstCustomerType = getAllCustomerType();
            }
            if (lstCustomerType.Count > 1)
                Index = 1;
        }
        public CustomerType(bool a)
        {

            Picture = new Image();
            ID = IDGenerate(Tables.CustomerType);
            if (lstCustomerType == null)
            {
                lstCustomerType = getAllCustomerType();
            }
        }
        public CustomerType(String _ID)
        {
            ID = _ID;
            Picture = new Image();
            PutTableIntoDataSet(Tables.CustomerType);
            var tbl = ds.Tables[Tables.CustomerType];
            var row = tbl.Select(CustomerTypeTable.ID + "='" + ID + "'")[0];
            ID = row[CustomerTypeTable.ID] as string;
            Name = row[CustomerTypeTable.Name] as string;
            MinPoint = (int)row[CustomerTypeTable.MinPoint];
            Discount = (double)row[CustomerTypeTable.Discount];
            try { Picture = new Image(row[CustomerTypeTable.Picture] as string); }
            catch { }
            if (lstCustomerType == null)
            {
                lstCustomerType = new ObservableCollection<CustomerType>();
                lstCustomerType = getAllCustomerType();
            }
            for (int i = 0; i < lstCustomerType.Count; ++i)
                if (ID == lstCustomerType[i].ID) Index = i;
        }
        public static ObservableCollection<CustomerType> getAllCustomerType()
        {
            var lst = new ObservableCollection<CustomerType>();
            PutTableIntoDataSet(Tables.CustomerType);
            var tbl = ds.Tables[Tables.CustomerType];
            var Rows = tbl.Select();
            lst = new ObservableCollection<CustomerType>();
            foreach (var row in Rows)
                lst.Add(new CustomerType(row[CustomerTypeTable.ID] as string));
            return lst;
        }
        public void Save()
        {
            #region Lưu Loại Khách Hàng
            using (var tbl = ds.Tables[Tables.CustomerType])
            {
                var Rows = ds.Tables[Tables.CustomerType].Select(CustomerTypeTable.ID + "='" + ID + "'");
                if (Rows.Length == 0)
                {
                    var LastestRow = tbl.Select().Last();
                    var LastestID = LastestRow[CustomerTypeTable.ID] as String;
                    ID = IDGenerate(Tables.CustomerType);
                    var Row = tbl.NewRow();
                    Row[CustomerTypeTable.ID] = ID;
                    Row[CustomerTypeTable.Name] = Name;
                    Row[CustomerTypeTable.MinPoint] = MinPoint;
                    Row[CustomerTypeTable.Discount] = Discount;
                    Row[CustomerTypeTable.Picture] = Picture.ToBase64String();
                    tbl.Rows.Add(Row);

                    SaveCommand(Tables.CustomerType);
                }
                else
                {
                    foreach (System.Data.DataRow Row in tbl.Rows)
                        if ((Row[CustomerTypeTable.ID] as String) == ID)
                        {
                            Row[CustomerTypeTable.Name] = Name;
                            Row[CustomerTypeTable.MinPoint] = MinPoint;
                            Row[CustomerTypeTable.Discount] = Discount;
                            Row[CustomerTypeTable.Picture] = Picture.ToBase64String();
                            SaveCommand(Tables.CustomerType);
                            break;
                        }
                }
            }
            #endregion
        }
        public void AddPhoto()
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Image|*.jpg";
            if (dialog.ShowDialog().Value)
            {
                MemoryStream memoryStream = new MemoryStream();
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(new BitmapImage(new Uri(dialog.FileName))));
                encoder.Save(memoryStream);
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.EndInit();
                Picture.DisplayImage = bitmapImage;
            }
        }
        public void Delete()
        {
            using (var tbl = ds.Tables[Tables.CustomerType])
            {
                string Condition = CustomerTypeTable.ID + "='" + ID + "'";
                tbl.Select(Condition)[0].Delete();
                SaveCommand(Tables.CustomerType);
            }
        }
    }
}
